//
//  UIView+ThisObserver.swift
//  CMDB
//
//  Created by 王磊 on 15/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit

private var unsureOberversKey : String = "unsureOberversKey"

extension UIView {
    
    var unsureObervers: Array<CMDBThisObserverBean>? {
        set{

            objc_setAssociatedObject(self, &unsureOberversKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        get{
            return objc_getAssociatedObject(self, &unsureOberversKey) as? Array<CMDBThisObserverBean>
        }
    }
}
extension UIView {
    
    internal func addTarget(target: AnyObject,action: Selector,forEvent itemClickType: TSItemClickType) {
        
        // 1.函数名 2.参数个数 3.参数类型
        let observer = CMDBThisObserverBean()
        
        observer.target = target
        
        observer.action = action
        
        observer.itemClickType = itemClickType
        
        if unsureObervers == nil {
            
            unsureObervers = []
        }
        
        unsureObervers! += [observer]
        

    }
    
    internal func performSelWithArgu(argu: TSArguInfo) {
        
        guard unsureObervers != nil else {
            
            return
        }
        
        _ = unsureObervers!.map({
            
            if $0.itemClickType == argu.itemType {
                
                $0.target!.performSelector($0.action!, withObject: argu)
            }
        })
    }
}